package org.androidpn.fragment;

import java.util.ArrayList;
import java.util.List;

import org.androidpn.R;
import org.androidpn.activity.MainActivity;
import org.androidpn.activity.SearchResultActivity;
import org.androidpn.activity.UserInfoDetailActivity;
import org.androidpn.adapter.RecommendGVAdapter;
import org.androidpn.http.HttpApi;
import org.androidpn.model.CUserInfo;
import org.androidpn.model.NetModel;
import org.androidpn.utils.AppCache;
import org.androidpn.utils.ConstanceUtil;
import org.androidpn.utils.JsonUtil;
import org.androidpn.utils.ToastUtil;
import org.androidpn.widget.NoScrollGridView;

import com.android.volley.VolleyError;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment3 extends Fragment implements OnItemClickListener, OnClickListener {

	NoScrollGridView recmmendGv1;
	NoScrollGridView recmmendGv2;
	private int mTopPage = 1;
	private int mTopSize = 8;

	private int mPersonalPage = 1;
	private int mPersonalSize = 8;
	private List<CUserInfo> cUserInfosRecommend = new ArrayList<CUserInfo>();
	private List<CUserInfo> cUserInfosRecommendTag = new ArrayList<CUserInfo>();
	private RecommendGVAdapter reGvAdapter;
	private RecommendGVAdapter retagGvAdapter;
	private LinearLayout searchLy;
	private TextView tvTopChange;
	private TextView tvPersonChange;

	private RecmmondResponseListener recmmondResponseListener;
	private RecmmondTagResponseListener recmmondTagResponseListener;

	public Fragment3() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		View view = inflater.inflate(R.layout.fragment_fragment3, container, false);
		recmmendGv1 = (NoScrollGridView) view.findViewById(R.id.recommend_gv1);
		recmmendGv2 = (NoScrollGridView) view.findViewById(R.id.recommend_gv2);
		searchLy = (LinearLayout) view.findViewById(R.id.search_ly);
		searchLy.setOnClickListener(this);
		// etSearch =(EditText) view.findViewById(R.id.et_search);
		// ivSearch =(ImageView) view.findViewById(R.id.iv_search);
		reGvAdapter = new RecommendGVAdapter(getActivity());
		retagGvAdapter = new RecommendGVAdapter(getActivity());
		reGvAdapter.setcUserInfos(cUserInfosRecommend);
		retagGvAdapter.setcUserInfos(cUserInfosRecommendTag);
		recmmendGv1.setAdapter(reGvAdapter);
		recmmendGv2.setAdapter(retagGvAdapter);
		recmmendGv1.setOnItemClickListener(this);
		recmmendGv2.setOnItemClickListener(this);
		tvTopChange = (TextView) view.findViewById(R.id.tv_top_recommend);
		tvPersonChange = (TextView) view.findViewById(R.id.tv_personal_recommend);
		// ivSearch.setOnClickListener(this);
		recmmondResponseListener = new RecmmondResponseListener();
		recmmondTagResponseListener = new RecmmondTagResponseListener();
		tvTopChange.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mTopPage += 1;
				HttpApi.recommendFriend(mTopPage + "", mTopSize + "", recmmondResponseListener,
						recmmondResponseListener);
			}
		});

		tvPersonChange.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPersonalPage += 1;
				HttpApi.recommendFriendTag(((MainActivity) getActivity()).cUserInfo.getTag(), mPersonalPage + "",
						mPersonalSize + "", recmmondTagResponseListener, recmmondTagResponseListener);
			}
		});
		CUserInfo userInfo = AppCache.getInstance().getUseInfo();
		if (userInfo != null) {
			HttpApi.recommendFriend(mTopPage + "", mTopSize + "", recmmondResponseListener, recmmondResponseListener);
			HttpApi.recommendFriendTag(userInfo.getTag(), mPersonalPage + "", mPersonalSize + "",
					recmmondTagResponseListener, recmmondTagResponseListener);
		}
		return view;
	}

	@Override
	public void onResume() {
		CUserInfo userInfo = AppCache.getInstance().getUseInfo();
		if (userInfo != null) {
			HttpApi.recommendFriend(mTopPage + "", mTopSize + "", recmmondResponseListener, recmmondResponseListener);
			HttpApi.recommendFriendTag(userInfo.getTag(), mPersonalPage + "", mPersonalSize + "",
					recmmondTagResponseListener, recmmondTagResponseListener);
		}
		super.onResume();
	}

	class RecmmondResponseListener
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onErrorResponse(VolleyError error) {
			// TODO Auto-generated method stub
			ToastUtil.showShortToast(getActivity(), "网络异常");
		}

		@Override
		public void onResponse(String response) {
			// TODO Auto-generated method stub
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS) {
				Log.e("wulianshu", "userinfocommend:" + netModel.getContent());
				cUserInfosRecommend = JsonUtil.parseArray(netModel.getContent(), CUserInfo.class);
				if (cUserInfosRecommend == null || cUserInfosRecommend.size() <= 0
						|| cUserInfosRecommend.size() < mTopSize) {
					mTopPage = 1;
				}
				// 更新界面
				reGvAdapter.setcUserInfos(cUserInfosRecommend);
				reGvAdapter.notifyDataSetChanged();
			} else {
				mTopPage = 1;
				// ToastUtil.showShortToast(Fragment3.this.getActivity(),
				// netModel.getContent());
			}
		}
	}

	class RecmmondTagResponseListener
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onErrorResponse(VolleyError error) {
			// TODO Auto-generated method stub
			ToastUtil.showShortToast(getActivity(), "网络异常");
		}

		@Override
		public void onResponse(String response) {
			// TODO Auto-generated method stub
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS) {
				Log.e("wulianshu", "cUserInfosRecommendTag:" + netModel.getContent());
				cUserInfosRecommendTag = JsonUtil.parseArray(netModel.getContent(), CUserInfo.class);
				if (cUserInfosRecommendTag == null || cUserInfosRecommendTag.size() <= 0
						|| cUserInfosRecommendTag.size() < mPersonalSize) {
					mPersonalPage = 1;
				}
				// 更新界面
				retagGvAdapter.setcUserInfos(cUserInfosRecommendTag);
				retagGvAdapter.notifyDataSetChanged();
			} else {
				mPersonalPage = 1;
				// ToastUtil.showShortToast(Fragment3.this.getActivity(),
				// netModel.getContent());
			}

		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.recommend_gv1:

			Intent intent = new Intent(getActivity(), UserInfoDetailActivity.class);
			intent.putExtra("cuserinfo", cUserInfosRecommend.get(arg2));
			startActivity(intent);
			break;
		case R.id.recommend_gv2:
			Intent intent2 = new Intent(getActivity(), UserInfoDetailActivity.class);
			intent2.putExtra("cuserinfo", cUserInfosRecommendTag.get(arg2));
			startActivity(intent2);
			break;
		}
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.search_ly:
			Intent intent = new Intent(getActivity(), SearchResultActivity.class);
			startActivity(intent);
			break;
		}
	}
}
